import nci60.data.connected.LeadingProtein
import nci60.data.maxquant.Project

//Quering total number of identified genes
println 'Genes (total):\t' + LeadingProtein.executeQuery('select count(distinct g.name)from GeneName g, LeadingProtein l where l.protein=g.protein')[0]

//Quering total number of identified genes per experiment (project)
LeadingProtein.executeQuery('select l.currentProject, count(distinct g.name)from GeneName g, LeadingProtein l where l.protein=g.protein group by l.currentProject').each{line->
    println 'Genes ('+line[0].name+'):\t'+line[1]
}
println ''

//Data for Venn-Diagramm
//get all experiments (projects) and assign an alias
def counter = 1
def projectIdToAlias = [:]
println 'Alias\tExperiment'
Project.findAll().sort(){it.id}.each{
    println '' + counter + '\t' + it.name
    projectIdToAlias.put(it.id, counter)
    counter++
}
println ''

//get all genes and corresponding experiments (projects), in which those were identified
def tempData = [:]      //tempProtein = [gene:[projectIDs]
LeadingProtein.executeQuery('select l.currentProject, g.name from GeneName g, LeadingProtein l where l.protein=g.protein').each(){line->
    def proj = line[0]
    def prot = line[1]
    if(!tempData.containsKey(prot)){tempData.put(prot,[:])}
    tempData[prot].put(projectIdToAlias[proj.id], 0)
}

//form all sets of experiments (projects) and count the identified genes
def combinations = [:]  //[set:numberOfIdentifiedGenes]
tempData.each(){prot, projs->
    def combinationKey = projs.keySet().sort().toString()[1..-2]
    if(!combinations.containsKey(combinationKey)){combinations.put(combinationKey,0)}
    combinations[combinationKey]++
}

//print the resulting sets and the number of identified genes
println 'Set of experiments\tNumber of identified genes'
combinations.sort().each(){key,count->
    println '' + key + '\t' + count
}